package com.zzt.appframe8;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Editable;
import android.text.TextWatcher;
import android.text.method.PasswordTransformationMethod;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;


/**
 * @author: zeting
 * @date: 2020/11/9
 * 带眼睛的密码框
 */
public class PasswordIconEditText extends LinearLayout {
    private DeleteIconEditText et_eye_password;
    private ImageView iv_eye_icon;

    private boolean hasShow = false;

    public PasswordIconEditText(Context context) {
        super(context);
        init(context);
    }

    public PasswordIconEditText(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PasswordIconEditText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.password_edittext_eye, this, true);
        et_eye_password = findViewById(R.id.et_eye_password);
        iv_eye_icon = findViewById(R.id.iv_eye_icon);
        et_eye_password.setTypeface(Typeface.DEFAULT);

        hasShow = false;
        if (hasShow) {
            iv_eye_icon.setBackgroundResource(R.mipmap.icon_password_eye_show);
        } else {
            iv_eye_icon.setBackgroundResource(R.mipmap.icon_password_eye_hide);
        }
        iv_eye_icon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //从隐藏变显示
                if (hasShow) {
                    hasShow = false;
                    passwordState(et_eye_password, hasShow);
                    iv_eye_icon.setBackgroundResource(R.mipmap.icon_password_eye_hide);
                } else {
                    hasShow = true;
                    passwordState(et_eye_password, hasShow);
                    iv_eye_icon.setBackgroundResource(R.mipmap.icon_password_eye_show);
                }
            }
        });

        et_eye_password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.d("TAG", "输入文本前 beforeTextChanged  s：" + s + " -start " + start + " -count " + count + " -after " + after);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.d("TAG", "输入文本前 onTextChanged  s：" + s + " -start " + start + " -count " + count + " -before " + before);
//                if (count > 0) {
//                    CharSequence ts = s.subSequence(start, start + count);
//                    for (int i = 0; i < ts.length(); i++) {
//                        if (StringUtil.isChinese(ts.charAt(i))) {
//                            et_eye_password.setText(s.subSequence(0, start));
//                            et_eye_password.setSelection(et_eye_password.getText().length());
//                            return;
//                        }
//                    }
//                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.d("TAG", "输入文本前 afterTextChanged  s：" + s);
            }
        });

    }

    /**
     * 隐藏显示密码状态
     *
     * @param editText
     * @param hasShow  true 明文， false 密文
     */
    public void passwordState(EditText editText, boolean hasShow) {
        final int selection = editText.getSelectionEnd();
        if (hasShow) {
            editText.setTransformationMethod(null);
        } else {
            editText.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        if (selection >= 0) {
            editText.setSelection(selection);
        }
    }

    /**
     * 设置下划线
     *
     * @param lineView
     * @param underscoreCheckColor
     * @param underscoreUnCheckColor
     */
    public void setUnderscoreCheckColor(View lineView, int underscoreCheckColor, int underscoreUnCheckColor) {
        if (et_eye_password != null) {
            et_eye_password.setUnderscoreCheckColor(lineView, underscoreCheckColor, underscoreUnCheckColor);
        }
    }

    public void addDeleteFocusListener(DeleteIconEditText.DeleteEditTextFocusListener deleteFocusListener) {
        if (et_eye_password != null) {
            et_eye_password.addDeleteFocusListener(deleteFocusListener);
        }
    }

    public void setText(CharSequence text) {
        et_eye_password.setText(text);
    }

    public Editable getText() {
        return et_eye_password.getText();
    }

    public EditText getEditText() {
        return et_eye_password;
    }

    /**
     * 设置错误信息
     *
     * @param textView
     * @param error
     */
    public void setErrorInfo(TextView textView, String error) {
        if (textView != null) {
            textView.setVisibility(View.VISIBLE);
            textView.setText(error);
        }
        if (et_eye_password != null) {
            et_eye_password.setErrorInfo(true);
            et_eye_password.addDeleteChangedListener(text -> {
                if (textView != null) {
                    textView.setVisibility(View.GONE);
                }
            });
        }

    }

}
